cs2dfandomcom-20200223-history
Lua Tutorial
This tutorial is both a crash course in the language and our first script ever written in Counter-Strike 2D. By the end we will have a small Lua script which will be pridefully yours. Lets start! Reserved words Lua, as all other programming languages, has some reserved keywords. Here's a list of those words: end -- must match blocks that need to be closed that were created using if, for, while, do, function if and or not then elseif else for while do repeat until break true false function return Here's a list of the default non-overloaded operators: = --Assignment (assigns right operand to left operand ie. a = 5) --Equal to ~= --Not equal to > --Bigger than (only for numbers) < --Less than (only for numbers) >= --Bigger or equal to (only for numbers) <= --Less or equal to (only for numbers) + --Add (only for numbers) - --Subtract (only for numbers) * --Multiply (only for numbers) / --Divide (only for numbers) ^ --Raise to power (only for numbers) # --Length operator (only for tables and strings) .. --Concatenation operator (only for strings and numbers through coertion) n --indexation operator (only for tables, will access the nth element) Variables Variables can be used to store numbers and strings, and later work with them. As their name suggests, they can be changed any time, even during a Lua script. First, they have to be defined at the very start of the code. Defining them is easy: spawned=0 A variable named "spawned" has been created, and its value is currently 0. The variable later can be increased - for example, by 1 - by making them equal with themselves + 1, something like this: spawned=spawned+1 It forces the "spawned" variable to change its value to itself value plus one, so, it increases itself by 1, thus it now has the value 1. You can also decrease, multiply, divide variables. You can raise them to power, find their square root and much more. But variables are not necessarily numbers. You can give variables string values. Here, for example: spawnedtxt="none" Variables can also have boolean values - true and false. You can check if a variable is true by doing this: var=true if (var) then msg("Harr") end To check if a variable is false: var=false if (not var) then msg("Durr") end Functions Functions are like subprograms, helpers that will do all the work and save your time. You can give a function no, one or numerous arguments. Let's see examples - this function will print an info about how many spawns were there and increase our "spawned" variable by one: function sayspawns() spawned=spawned+1 msg("Current number of spawns is "..spawned) end So now, let's call it (as you remember, our spawned variable was 1): sayspawns() And we see "Current number of spawns is 2" printed and our spawned variable is now 2. Hooks You can also assign hooks to functions. What is called a 'hook' in Lua, in other languages is called 'flag' or 'event'. By adding a hook to a function it will be called automatically when the hook's event happened. For example the hook 'spawn' will call an assigned function when some player has spawned. You can assign the same hook to numerous functions, and vice versa - you can assign multiple hooks to one function. Now, let's finally finish our script. As we have already made the function to increase spawns and show the number of them, all we have to do is to add a hook to this function! addhook("spawn","sayspawns") Finish addhook(youarenoob,younoob) function left thisforum be as containeer Other notes CS2D Says You : Big Shit Holyday Other tutorials *FN_Linkin Park's tutorial *TheKilledDeath's tutorial Category:Lua Category:Tutorials